﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;

using NPOI.SS;
using NPOI.SS.UserModel;
using NPOI.XSSF;
using NPOI.XSSF.UserModel;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Smartmanufacturing;
using VOL.Smartmanufacturing.DTO.Bom;
using VOL.Smartmanufacturing.DTO.SmartDisplayWall;
using VOL.Smartmanufacturing.IServices;

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace VOL.WebApi.Controllers
{
    /// <summary>
    /// 物料管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaterialManagementController : ControllerBase
    {
        /// <summary>
        /// bom接口
        /// </summary>
        private readonly IBOMService ibomService;

        public MaterialManagementController(IBOMService ibomService)
        {
            this.ibomService = ibomService;
        }

        [HttpGet]
        /// <summary>
        /// 显示bom数据
        /// </summary>
        /// <param name="getBomDto">查询分页参数</param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetBom([FromQuery]GetBomDto getBomDto)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.GetBom(getBomDto);
            return webResponseContent.OK("数据查询成功", result);
        }
        [HttpPost]
        /// <summary>
        /// 添加Bom
        /// </summary>
        /// <param name="bom"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> AddBom([FromBody]BOM bom)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.AddBom(bom);
            return webResponseContent.OK("添加bom数据成功", result);
        }
        [HttpPost]
        /// <summary>
        /// 删除Bom
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> DelBom([FromBody]string ids)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.DelBom(ids);
            return webResponseContent.OK("删除bom数据成功", result);
        }
        [HttpPost]
        /// <summary>
        /// 修改bom数据
        /// </summary>
        /// <param name="bom">bom实体</param>
        /// <returns></returns>
        public async Task<WebResponseContent> UpDateBom([FromBody]BOM bom)
        {

            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.UpDateBom(bom);
            return webResponseContent.OK("修改bom数据成功", result);
        }

        [HttpPost]
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> AddBOMlist([FromBody]List<BOM> bomlist)
        {

            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.AddBOMlist(bomlist);
            return webResponseContent.OK("批量添加bom数据成功", result);
        }



        [HttpPost]

        /// <summary>
        /// 导入BOM数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>

        [HttpPost]

        public async Task<IActionResult> ImportExcel(IFormFile file)
        {
            try
            {
                //创建一个内存对象
                MemoryStream ms = new MemoryStream();
                file.CopyTo(ms);
                ms.Position = 0;

                //创建工作簿
                IWorkbook b = new XSSFWorkbook(ms);
                //获取第一个工作表
                ISheet s = b.GetSheetAt(0);
                //获取总行数
                int totalRows = s.LastRowNum;
                //创建房号集合

                List<BOM> list = new List<BOM>();

                //循环总行数往list添加数据


               

                for (int i = 1; i <= totalRows; i++)

                {
                    //获取每一行每个单元格的数据
                    IRow row = s.GetRow(i);
                    string BomCode = row.GetCell(0).ToString();
                    string MaterialNumber = row.GetCell(1).ToString();
                    string MaterialName = row.GetCell(2).ToString();
                    string Supplier = row.GetCell(3).ToString();
                    string VersionNumber = row.GetCell(4).ToString();
                    string States = row.GetCell(5).ToString();
                    string Remarks = row.GetCell(6).ToString();


                    BOM info = new BOM()
                    {
                        BomCode = BomCode,
                        MaterialNumber = MaterialNumber,
                        MaterialName = MaterialName,
                        Supplier = Supplier,
                        VersionNumber = VersionNumber,
                        States = int.Parse(States),
                        Remarks = Remarks,
                    };

                    list.Add(info);
                }

                b.Close();


                int res = await ibomService.AddBOMlist(list);

                return Ok(res);

                


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        [HttpGet]
        /// <summary>
        /// 显示物料信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetMaterial([FromQuery]GetMaterialDTO dto)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.GetMaterial(dto);
            return webResponseContent.OK("显示物料数据成功", result);
        }


        [HttpGet]
        /// <summary>
        /// 一周不良品统计
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> GetSmartDisplayWall(int days = 7)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.GetSmartDisplayWall(days);
            return webResponseContent.OK("显示一周不良品统计成功", result);
        }

        [HttpGet]
        /// <summary>
        ///订单今日进度
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> GetDailyPlanModel()
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.GetDailyPlanModel();
            return webResponseContent.OK("显示订单今日进度", result);
        }
        [HttpGet]
        /// <summary>
        /// 获取当前班组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetTeam()
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var result = await ibomService.GetTeam();
            return webResponseContent.OK("显示当前班组", result);
        }
    }

    }
